<?php

namespace App\Admin\Forms\User;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UserPaycodeForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return $this->response()
                ->error('无权限.');
        }

        $id = $input['id'];
        $payword = trim($input['payword'] ?? '');
        if(strlen($payword) != 6){
            return $this->response()
                ->warning('密码长度不为6')
                ->refresh();
        }
        update_user_meta($id, 'paycode', $payword);

        return $this->response()
            ->success('操作成功.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);
        $this->text('payword', '支付密码')->help('6位数字')->default(get_user_meta($this->payload["id"], 'paycode', 's'));
    }

}
